<?php
/**
 * CreateTime: 2022/5/7 14:19
 */

namespace App\common;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Models\Dictionary\DictionaryModel;
use Illuminate\Http\Exceptions\HttpResponseException;

class cDataF
{
    public DictionaryModel $dictionaryModel;

    public function __construct()
    {
        $this->dictionaryModel = new DictionaryModel();
    }

    /**
     * 根据字典ID获取字典详情
     * @param $dictionaryId
     * @param array|string[] $select
     * @return mixed
     */
    public function getDictionaryInfoByDictionaryId($dictionaryId,array $select = ["*"]){
        return $this->dictionaryModel->getDictionaryInfoByDictionaryId($dictionaryId,$select);
    }

    /**
     * 根据字典Key获取字典详情
     * @param $dictionaryKey
     * @param array|string[] $select
     * @return mixed
     */
    public function getDictionaryInfoByDictionaryKey($dictionaryKey,array $select = ["*"]){
        return $this->dictionaryModel->getDictionaryInfoByDictionaryKey($dictionaryKey,$select);
    }

    /**
     * 根据字典key获取字典子节点
     * @param $dictionaryKey
     * @param array|string[] $select
     * @return mixed
     */
    public function getDictionaryNodeByDictionKey($dictionaryKey,array $select = ["*"]){
        $parentId = @$this->dictionaryModel->getDictionaryInfoByDictionaryKey($dictionaryKey,['dictionary_id'])['dictionary_id'];
        if(!$parentId){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '节点未找到',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
        return $this->dictionaryModel->getDictionaryNodeByDictionaryId($parentId,$select);
    }

    /**
     * 根据字典ID获取字典子节点
     * @param $dictionaryId
     * @param array|string[] $select
     * @return mixed
     */
    public function getDictionaryNodeByDictionId($dictionaryId,array $select = ["*"]){
        $dictionaryModel = new DictionaryModel();
        return $this->dictionaryModel->getDictionaryNodeByDictionaryId($dictionaryId,$select);
    }

    /**
     * 通过父节点KEY 自动匹配子节点value
     * @param $parentDictionaryKey
     * @param $val
     * @return string
     */
    public function getDictionaryMapValueByParentKey($parentDictionaryKey,$val): string
    {
        $nodesKey = $this->getDictionaryNodeByDictionKey($parentDictionaryKey,[
            'dictionary_id',
            'dictionary_value',
            'dictionary_key',
            'dictionary_name',
        ]);
        $nodesKeyArray = $nodesKey->toArray();
        if(!$nodesKeyArray){
//            dd($parentDictionaryKey);
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '当前节点不是父节点',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }

        foreach($nodesKeyArray as $value){
            if($value['dictionary_value'] == $val){
                return $value['dictionary_name'];
            }
        }
        return "-";
    }

    /**
     * 字典名匹配
     * @param $parentDictionaryKey
     * @param $val
     * @return string
     */
    public function getDictionaryMapAuditValueByParentKey($parentDictionaryKey,$val): string
    {
        $nodesKey = $this->getDictionaryNodeByDictionKey($parentDictionaryKey,[
            'dictionary_id',
            'dictionary_value',
            'dictionary_key',
            'dictionary_audit_name'
        ]);
        $nodesKeyArray = $nodesKey->toArray();
        if(!$nodesKeyArray){
//            dd($parentDictionaryKey);
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '当前节点不是父节点',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }

        foreach($nodesKeyArray as $value){
            if($value['dictionary_value'] == $val){
                return $value['dictionary_audit_name'];
            }
        }
        return "-";
    }

    /**
     * @param $parentDictionaryKey
     * @param $val
     * @param $msg
     * @return bool
     */
    public function checkerDictionaryHasNodesByParentKey($parentDictionaryKey,$val,$msg){
        $nodesKey = $this->getDictionaryNodeByDictionKey($parentDictionaryKey,[
            'dictionary_id',
            'dictionary_value',
            'dictionary_key'
        ]);
        $nodesKeyArray = $nodesKey->toArray();
        if(!$nodesKeyArray){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '当前节点不是父节点',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }

        $flag = false;
        foreach($nodesKeyArray as $value){
            if($value['dictionary_value'] == $val){
                $flag = true;
            }
        }
        if(!$flag){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                $msg,
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
        return $nodesKeyArray;
    }

	/* get all sub node data by parent key from dictionary
	 *
	 */
	public static function getAllSubNodeDataByParentkey($parent_key){
		
		return (new DictionaryModel())->getAllSubNodeDataByParentkey($parent_key);
	}
}